package co.fitstart.weapp.api.web.controller.market;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.market.satisfaction.SatisfactionSurvey;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.weapp.api.entity.SatisfactionSurveyRequest;
import co.fitstart.weapp.api.service.SatisfactionSurveyService;
import co.fitstart.weapp.api.service.StudentService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 满意度调查
 * @author haiyan_xiao
 *
 */
@RestController
@RequestMapping("/satisfaction-survey")
public class SatisfactionSurveyController {

    private static final Logger logger = LoggerFactory.getLogger(SatisfactionSurveyController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private SatisfactionSurveyService satisfactionSurveyService;

    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult index(@RequestParam("unionid") String unionid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            Student student = studentService.load(user);
            SatisfactionSurvey survey = satisfactionSurveyService.load(student);
            
            if(survey == null) {
                httpResult.fail("500").message("加载数据失败");
            } else {
                httpResult.success()
                          .message("填写问卷成功")
                          .data("survey", survey);
            }
        } catch (Exception e) {
            logger.error("#SatisfactionSurveyController index() occurs error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

    /**
     * 
     * @param survey
     * @param coachServices
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult fillIn(@RequestParam("unionid") String unionid,
                             @RequestBody SatisfactionSurveyRequest surveyRequest) {
        HttpResult httpResult = new HttpResult();
        logger.error("#SatisfactionSurveyController request() occurs error", JsonUtils.toJson(surveyRequest));
        
        try {
            if(surveyRequest.isValid()) {
                User user = userService.load(unionid);
                Student student = studentService.load(user);
                SatisfactionSurvey survey = satisfactionSurveyService.load(student);
                
                if(survey.getId().equals(surveyRequest.getId())) {
                    satisfactionSurveyService.update(survey.edit(surveyRequest.getCity(), surveyRequest.getDistrict(),
                            surveyRequest.getSiteAnswerOne(), surveyRequest.getSiteAnswerTwo(), surveyRequest.getSiteAnswerThree(), surveyRequest.getSiteAnswerFour(),
                            surveyRequest.getServerAnswerOne(), surveyRequest.getServerAnswerTwo(), surveyRequest.getServerAnswerThree(),
                            surveyRequest.getServerRemarkThree(), surveyRequest.getServerAnswerFour(), surveyRequest.getServerRemarkFour(),
                            surveyRequest.getServerAnswerFive(), surveyRequest.getServerAnswerSix(),
                            surveyRequest.getCoachAnswerOne(), surveyRequest.getCoachAnswerTwo(), surveyRequest.getCoachAnswerThree(),
                            surveyRequest.getCoachAnswerFour(), surveyRequest.getCoachAnswerFive(), surveyRequest.getCoachAnswerSix(),
                            surveyRequest.getCoachAnswerSeven(), surveyRequest.getCoachServices(),
                            surveyRequest.getTotalAnswerOne(), surveyRequest.getTotalAnswerTwo(), surveyRequest.getTotalAnswerThree(),
                            surveyRequest.getTotalAnswerFour(), surveyRequest.getTotalAnswerFive(), surveyRequest.getTotalAnswerSix(),
                            surveyRequest.getTotalAnswerSeven(), surveyRequest.getTotalAnswerEight(), surveyRequest.getTotalAnswerNine()));
                    
                    httpResult.success()
                              .message("提交问卷成功");
                } else {
                    httpResult.fail("500").message("提交问卷失败");
                }
            } else {
                httpResult.fail("500").message("请先填完问卷");
            }
            
        } catch (Exception e) {
            logger.error("#SatisfactionSurveyController fillIn() occurs error", e);
            httpResult.fail("500").message("提交问卷失败");
        }
        
        return httpResult;
    }
}
